/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package df.servlet.process;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import df.bean.db.conn.DBConnection;
import df.bean.db.table.Batch;
//import df.bean.obj.doctor.DoctorList;
import df.bean.obj.util.JDate;
import df.bean.process.ProcessImport;
import df.bean.process.ProcessJoinBill;
import df.bean.process.ProcessReceipt;

/**
 *
 * @author admin
 */
public class ProcessImportBillSrvl extends ProcessServlet {
    @Override
    public void init() throws ServletException {
    	/*
        super.init();

        conn = new DBConnection();
        conn.connectToServer();
        
        System.out.println("------------------ initial ------------------");
        System.out.println("Start time : " + JDate.getTime());
        */
    }
    private void newObject() {
    	/*
        batch = new Batch(this.conn.getHospitalCode(), conn);
//        drList = new DoctorList(Variables.getHospitalCode(), conn);
        pr = new ProcessReceipt(conn);
        pi = new ProcessImport(conn);
        System.out.println ("========  new object =======");
        */
    }

    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */

    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {              
        response.setContentType("text/xml; charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        
        request.setCharacterEncoding("UTF-8");
        
        String user = request.getParameter("USER");
        String password = request.getParameter("PWD");
        String hospitalCode = request.getParameter("HOSPITAL_CODE");
        String startDate = JDate.saveDate(request.getParameter("START_DATE"));
        String endDate = JDate.saveDate(request.getParameter("END_DATE"));
        String type = request.getParameter("TYPE");
    	DBConnection conn = new DBConnection();
    	conn.connectToLocal();
    	Batch batch = new Batch(hospitalCode, conn);
    	ProcessImport pi = new ProcessImport(conn);
    	ProcessReceipt pr = new ProcessReceipt(conn);
        ProcessJoinBill j = new ProcessJoinBill();
        int numAffRec = 0;

        conn.setHospitalCode(hospitalCode);
        conn.setUserID(user);
        conn.setPassword(password);
        
        if (conn.getConnection() == null) {
            out.print("<RESULT><SUCCESS>" + 0 + "</SUCCESS></RESULT>");
            return ;
        }
        
        if (pr == null) {       
            newObject();
        }
        
        conn.beginTrans();
        if (type.equals("Bill Transaction")) {
        	j.setHospitalCode(hospitalCode);
            if(j.ProcessJoinBill(startDate, endDate)){
                if (pi.importBill(hospitalCode, startDate, endDate) && 
                	pi.updatePrivateDoctor_BILL(hospitalCode, startDate, endDate) &&
                    pr.CalculateReceiptByCash(Batch.getYyyy(), Batch.getMm(), startDate, endDate, hospitalCode)) {
                numAffRec = 1;
                } else numAffRec = 0;
            }else numAffRec = 0;
        }
		
        if (type.equals("Patho Transaction")) {
            if(true){
                if (pi.importPatho(hospitalCode, startDate, endDate) && 
                    pr.CalculateReceiptByCash(Batch.getYyyy(), Batch.getMm(), startDate, endDate, hospitalCode)) {
                numAffRec = 1;
                } else numAffRec = 0;
            }else numAffRec = 0;
        }
        
        if (type.equals("Verify Transaction")) {
            if (pi.importVerifyInMonth(hospitalCode, startDate, endDate) && 
                    pi.importVerifyRecOverMonth(hospitalCode, startDate, endDate) &&
                    pi.importVerifyNotRec(hospitalCode, startDate, endDate) &&
                    pi.updatePrivateDoctor_BILL(hospitalCode, startDate, endDate)) {
                numAffRec = 1;
            } else numAffRec = 0;
        }
        if(pi.updateStpMappingRule(hospitalCode, startDate, endDate)){
        	numAffRec = 1;
        }else{
        	numAffRec = 0;
        }
            
        if (numAffRec == 1) conn.commitTrans();
        if (numAffRec == 0) conn.rollBackTrans();
        
        try {
            Thread.sleep(20);
            out.print("<RESULT><SUCCESS>" + numAffRec + "</SUCCESS></RESULT>");
        }
        catch (Exception  e) {
            e.printStackTrace(out);
        }
        finally { 
            out.close();
            conn.Close();
        }
        
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
